package com.haixiaoke.business;

import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockPwd;
import com.haixiaoke.service.ISmartLockPwdService;
import com.haixiaoke.service.ISmartLockService;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKEventHandleService;
import com.haixiaoke.smartLock.huixiangjia.SmartLockHxjEventHandleService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlEventHandleService;
import com.haixiaoke.smartLock.yaluete.SmartLockYLTEventService;
import com.haixiaoke.smartLock.youdian.SmartLockYdEventHandleService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能锁密码授权信息Controller
 *
 * @author xiaolong
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/business/password")
public class SmartLockPwdController extends BaseController {
    @Autowired
    private ISmartLockPwdService smartLockPwdService;
    @Autowired
    private ISmartLockService smartLockService;
    @Autowired
    private SmartLockTwhlEventHandleService smartLockTwhlEventHandleService;
    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;
    @Autowired
    private SmartLockYdEventHandleService smartLockYdEventHandleService;
    @Autowired
    private SmartLockHXKEventHandleService smartLockHXKEventHandleService;
    @Autowired
    private SmartLockHxjEventHandleService smartLockHxjEventHandleService;

    /**
     * 查询智能锁密码授权信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLockPwd smartLockPwd) {
        startPage();
        List<SmartLockPwd> list = smartLockPwdService.selectSmartLockPwdList(smartLockPwd);
        return getDataTable(list);
    }

    /**
     * 导出智能锁密码授权信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLockPwd smartLockPwd) {
        List<SmartLockPwd> list = smartLockPwdService.selectSmartLockPwdList(smartLockPwd);
        ExcelUtil<SmartLockPwd> util = new ExcelUtil<SmartLockPwd>(SmartLockPwd.class);
        util.exportExcel(response, list, "智能锁密码授权信息数据");
    }

    /**
     * 获取智能锁密码授权信息详细信息
     */
    @GetMapping(value = "/{pwdId}")
    public AjaxResult getInfo(@PathVariable("pwdId") Long pwdId) {
        return success(smartLockPwdService.selectSmartLockPwdByPwdId(pwdId));
    }

    /**
     * 新增智能锁密码授权信息和智能锁密码授权
     *
     * @param smartLockPwd 智能锁密码授权信息
     * @return 结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLockPwd smartLockPwd) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockPwd.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.insertSmartLockPwd(smartLockPwd));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.insertSmartLockPwd(smartLockPwd));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.insertSmartLockPwd(smartLockPwd));
        } else if(smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYdEventHandleService.insertSmartLockPwd(smartLockPwd));
        } else  if(smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK){
            return toAjax(smartLockHxjEventHandleService.insertSmartLockPwd(smartLockPwd));
        } else {
            return error("暂不支持该品牌的智能锁");
        }
    }

    /**
     * 修改智能锁密码授权信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLockPwd smartLockPwd) {
        return toAjax(smartLockPwdService.updateSmartLockPwd(smartLockPwd));
    }

    /**
     * 删除智能锁密码授权信息
     */
    @DeleteMapping("/{pwdIds}")
    public AjaxResult remove(@PathVariable Long[] pwdIds) {
        return toAjax(smartLockPwdService.deleteSmartLockPwdByPwdIds(pwdIds));
    }

    /**
     * 删除智能锁密码授权信息和删除密码授权
     *
     * @param sn             智能锁sn
     * @param identification 天网互联智能锁标识
     * @return
     */
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam(value = "sn") String sn,
                             @RequestParam(value = "identification", required = false) String identification,
                             @RequestParam(value = "returnPwdId", required = false) Long returnPwdId) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.deleteSmartLockPwd(sn, identification));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.deleteSmartLockPwdBySnAndIdentification(sn, identification));
        } else if(smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.deleteSmartLockPwd(sn, identification));
        } else if(smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYdEventHandleService.deleteSmartLockPwd(returnPwdId));
        } else  if(smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK){
            return toAjax(smartLockHxjEventHandleService.deleteSmartLockPwd(sn, identification));
        } else {
            return error("暂不支持该品牌的智能锁");
        }
    }
}
